import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import app from './modules/app'
import settings from './modules/settings'
import user from './modules/user'
import router from '@/router'
import Layout from '@/layout'
import pageMap from '@/router/pageMap'
import iconClass from '@/router/iconClass'
import storage from '@/utils/storage'

import TTT from '@/layout/components/TTT'
import { findMenus } from '@/api/baseInfo/authority/resources'
import { findByUserIdAndSysId } from '@/api/baseInfo/authority/menu'
import { transData, showNotice } from '@/utils/toolclass'

Vue.use(Vuex)

function getCommonMenu() {
  const params = [
    {
      // isVisible: true,
      itemIconClass: 'iconfont icon-jigouzheshezhi',
      // parentId: 105,
      // resourceId: 76,
      displayType: 1,
      name: '机构设置',
      url: '@/views/orgSetup',
      children: [
        {
          // isVisible: true,
          itemIconClass: 'iconfont icon-jigouweihu',
          // parentId: 105,
          // resourceId: 76,
          displayType: 1,
          name: '机构维护',
          url: '/org/index'
        },
        {
          // isVisible: true,
          itemIconClass: 'iconfont icon-jigouweihu',
          // parentId: 105,
          // resourceId: 76,
          displayType: 1,
          name: '添加人员',
          url: '/addPeople/index'
        }
      ]
    },
    {
      // isVisible: true,
      itemIconClass: 'iconfont icon-shezhi',
      // parentId: 105,
      // resourceId: 76,
      displayType: 1,
      name: '安全设置',
      path: '@/views/baseInfo/safeSet',
      children: [
        {
          // isVisible: true,
          itemIconClass: 'iconfont icon-xiugaimima',
          // parentId: 105,
          // resourceId: 76,
          displayType: 1,
          name: '修改密码',
          url: '/modifyPwd'
        },
        {
          // isVisible: true,
          itemIconClass: 'iconfont icon-weixin',
          // parentId: 105,
          // resourceId: 76,
          displayType: 1,
          name: '绑定微信',
          url: '/bindWx'
        }
      ]
    }
  ]
  return params
}
const store = new Vuex.Store({
  state: {
    routerList: router.options.routes,
    sysId: null
  },
  mutations: {
    CHANGE_ROUTER: (state, val) => {
      state.routerList = val
    }
  },
  actions: {
    changeRouter({ commit, rootState }, obj) {
      const dtRoutes = []
      // console.log(obj)
      // param.systemId, param.resourceName
      // const sysId = obj
      const sysId = JSON.parse(storage.get('id'))
      // console.log(sysId)
      const result = findByUserIdAndSysId(sysId).then((res) => {
      // const result = findMenus(sysId).then((res) => {
        // console.log(res)
        // console.log('sysList======', res.data)
        if (res.code === '000000') {
          // this.dataList = res.data;
          // const getRouter = res.data
          storage.set('sysData', res.data)
          const getRouter = res.data
          getRouter.forEach(element => {
            // console.log('element')
            // console.log(element)
            element.itemIconClass = iconClass[element.name]
            // console.log('iconClass')
            // console.log(iconClass[element.resourceName])
          })
          // const routerArr = transData(res.data, 'resourceId', 'parentId')
          // console.log(routerArr)
          const routerArr = transData(res.data, 'id', 'parentId')
          // console.log(routerArr)
          // console.log('路由组件树')
          // console.log(routerArr[0].children)
          const loop = (ls, dirty = false) => {
            return ls.map(x => {
              let path = x.url
              let component
              // 最高层渲染布局组件
              if (!dirty) {
                component = Layout
                path = `/${path}`
              } else if (x.children && x.children.length > 0) { // 中间层渲染容器
                component = TTT
              } else { // 最底渲染自己对应的页面组件
                component = pageMap[x.name] || (() => import('@/views/platform'))
              }
              return {
                path,
                name: x.name,
                component,
                children: x.children ? loop(x.children, true) : [],
                meta: {
                  title: x.name,
                  icon: x.itemIconClass
                }
              }
            })
          }
          // console.log('store文件内获取')
          // console.log(rootState)
          // console.log(routerArr)
          // 操作指南
          const params = getCommonMenu()
          // console.log(params)
          for (var i = 0; i < params.length; i++) { routerArr[0].children.push(params[i]) }
          // console.log(routerArr)
          dtRoutes.push(...loop(routerArr[0].children))
          let RouterAll = rootState.routerList
          RouterAll = RouterAll.concat(dtRoutes)
          // console.log('所有路由', dtRoutes)
          // console.log(RouterAll)
          commit('CHANGE_ROUTER', RouterAll)
          // state.routerList.push(...loop(routerArr[0].children))
          router.addRoutes(dtRoutes)
          return res
        }
      })
      return result
      // commit('CHANGE_ROUTER', data)
    }
  },
  modules: {
    app,
    settings,
    user
  },
  getters
})

export default store
